CREATE PROC [dbo].[SupplementaryInsurance_CopyDrugCondition]
    @SourceInsurance VARCHAR(3),
    @Insurances VARCHAR(MAX)
AS
BEGIN TRY
    BEGIN TRANSACTION;
    DECLARE @Defined TABLE
    (
        GoodsCode VARCHAR(15) PRIMARY KEY,
        NonInsuranceAmount [MONEY],
        PatientAmount [MONEY],
        DifferAmount [MONEY],
        GoodsAmount [MONEY],
        MessageId [INT],
        Ceilling [INT]
    );
    INSERT INTO @Defined
    (
        GoodsCode,
        NonInsuranceAmount,
        PatientAmount,
        DifferAmount,
        GoodsAmount,
        MessageId,
        Ceilling
    )
    SELECT S.GoodsCode,
           S.NonInsuranceAmount,
           S.PatientAmount,
           S.DifferAmount,
           S.GoodsAmount,
           S.MessageId,
           S.Ceilling
    FROM SupplementaryInsurance_DrugCondition S
    WHERE InsuranceCode = @SourceInsurance;
    DELETE FROM dbo.SupplementaryInsurance_DrugCondition
    WHERE InsuranceCode IN
          (
              SELECT splitdata FROM dbo.fnSplitString(@Insurances, ',')
          );
    INSERT INTO dbo.SupplementaryInsurance_DrugCondition
    (
        GoodsCode,
        InsuranceCode,
        NonInsuranceAmount,
        PatientAmount,
        DifferAmount,
        GoodsAmount,
        MessageId,
        Ceilling
    )
    SELECT S.GoodsCode,
           I.splitdata,
           S.NonInsuranceAmount,
           S.PatientAmount,
           S.DifferAmount,
           S.GoodsAmount,
           S.MessageId,
           S.Ceilling
    FROM @Defined S
        CROSS APPLY dbo.fnSplitString(@Insurances, ',') I;

    COMMIT TRANSACTION;
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION;
    DECLARE @Err NVARCHAR(1000);
    SET @Err = N'RollBack ' + ERROR_MESSAGE();
    RAISERROR(@Err, 16, 1);
END CATCH;